<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../css/auislide.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <style type="text/css">
        html,
        body {
            background-color: #000;
            width: 100%;
            height: 100%;
        }
        #dlna {
            display:none;
        }
        .dlna {
            width: 30px;
            height: 30px;
            float: right;
            margin: 10px;
        }
    </style>
</head>
<body>
<header class="aui-bar aui-bar-nav" id="header" style="background: linear-gradient(#000, #000);color:#c4c4c4;position: relative;padding-top:25px;">
    <a class="aui-btn aui-pull-left" tapmode onclick="closeWin()">
        &nbsp; <span class="aui-iconfont aui-icon-left" style=color:#c4c4c4 ></span>
    </a>
    <div id="title" class="aui-title" >正在播放</div>
    <a id="play" class="aui-btn aui-pull-right" style=color:#c4c4c4 tapmode onclick="jieXi()">
        <i class="icon icon-genghuanxianlu-b"></i>  更换线路
    </a>
</header>
<!--投屏模块官方售价999元 有需要的联系我微信day176 低价渠道300-350-->
<!--投屏模块官方售价999元 有需要的联系我微信day176 低价渠道300-350-->
<!--投屏模块官方售价999元 有需要的联系我微信day176 低价渠道300-350-->
<a id=dlna tapmode onclick="browse()" class="" style="color: #c4c4c4;">
    <img src="../image/touping.png" class="dlna" />
    <b style="float:right;line-height:55px;font-size:0.5rem;">支持投屏电视端播放,点击投屏图标</b>
</a>
<!--投屏模块官方售价999元 有需要的联系我微信day176 低价渠道300-350-->
<!--投屏模块官方售价999元 有需要的联系我微信day176 低价渠道300-350-->
<!--投屏模块官方售价999元 有需要的联系我微信day176 低价渠道300-350-->
<input type="hidden" name="url1" id="url1" value="" />
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/date.min.js"></script>
<script type="text/javascript">
    var leCastDemo = null;
    var uid1 = "";
    var uzmoduledemo = null;
    var TVURL = "";
    var jx_url1 = localStorage.getItem('jx_url1');
    var jx_url2 = localStorage.getItem('jx_url2');
    apiready = function() {
        api.setScreenOrientation({ orientation:'auto' });
        leCastDemo = api.require('leCast');
        //alert(leCastDemo);
        leCastDemo.initLeCast(); //模块初始化方法，必须在所有方法调用前调用
        uzmoduledemo = api.require('androidBrowser');
        api.parseTapmode();
        var url1 = $api.byId('url1');
        url1.value = api.pageParam.url;
        var header = $api.byId('aui-header');
        $api.fixStatusBar(header);
        var headerPos = $api.offset(header);
        var body_h = $api.offset($api.dom('body')).h;
        api.addEventListener({
            name: 'keyback'
        }, function(ret, err) {
            api.setScreenOrientation({
                orientation: 'portrait_up'
            });
            api.closeWin();
        });
        var param = {
            /*
            x:浏览器窗口X位置，默认0
            y:浏览器窗口y位置，默认0
            w:浏览器宽度，默认满屏
            h:浏览器高度，默认满屏
            */
            rect: {
                x: 0,
                y: $api.dom('header').offsetHeight + 50,
                w: 'auto',
                h: 'auto',
            }, //w,h等于0代表横向满屏和纵向满屏
            fixedOn: "", //浏览器依附在哪个window,不传或传空 为 当前Window，默认当前当前Window
            fixed: true, //浏览器是否随frame或Window滑动，默认当前当前true
            url: api.pageParam.url, //要加载的url,可选项
            browserBg: "#F0F0F0", //可选
            timeout: 7, //超时时间,指加载页面完毕后等待多少秒，超过这个时间还没收到视频地址，则认为解析视频地址失败。默认7秒

        };
        uzmoduledemo.openView(param, function(ret, err) {
            //alert(JSON.stringify(ret));
            if (ret.result == '1') {
                TVURL = ret.VideoUrl;
                var dlna = $api.byId('dlna');
                dlna.style = "display:block";
                uzmoduledemo.closeView( function(ret, err){
                    //alert(JSON.stringify(ret));
                });
            }
            /*
            返回值：
            result：1找到视频，2加载完毕
            MainUrl：参数传入的url字段
            VideoUrl：解析到的视频地址

            */
        });
        var browser = api.require('webBrowser');
        browser.openView({
            url: api.pageParam.url,
            rect: {
                x: 0,
                y: $api.dom('header').offsetHeight + 45,
                w: 'auto',
                h: 'auto',
            }
        }, function(ret, err) {
            //alert(JSON.stringify(ret));
            switch (ret.state) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                default:
                    break;
            };
        });
        if (api.pageParam.needRecordHistory) {
            record_play_history();
        } else {
            //
        }
    }
    function getCaption(obj) {
        var index = obj.lastIndexOf("\=");
        obj = obj.substring(index + 1, obj.length);
        return obj;
    }
    function closeWin() {
        api.setScreenOrientation({
            orientation: 'portrait_up'
        });
        api.closeWin();
    }
    function jieXi(){
        api.actionSheet({
            title: '影视VIP线路',
            cancelTitle: '取消',
            buttons: ['VIP线路1', 'VIP线路2', 'VIP线路3', 'VIP线路4', 'VIP线路5', 'VIP线路6', 'VIP线路7', 'VIP线路8', 'VIP线路9']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if (ret.buttonIndex==1) {
                vipHuanxian(localStorage.getItem('jx_url2'));
            }
            if (ret.buttonIndex==2) {
                vipHuanxian(localStorage.getItem('jx_url3'));
            }
            if (ret.buttonIndex==3) {
                vipHuanxian(localStorage.getItem('jx_url4'));
            }
            if (ret.buttonIndex==4) {
                vipHuanxian(localStorage.getItem('jx_url5'));
            }
            if (ret.buttonIndex==5) {
                vipHuanxian(localStorage.getItem('jx_url6'));
            }
            if (ret.buttonIndex==6) {
                vipHuanxian(localStorage.getItem('jx_url7'));
            }
            if (ret.buttonIndex==7) {
                vipHuanxian(localStorage.getItem('jx_url8'));
            }
            if (ret.buttonIndex==8) {
                vipHuanxian(localStorage.getItem('jx_url9'));
            }
            if (ret.buttonIndex==9) {
                vipHuanxian(localStorage.getItem('jx_url10'));
            }
        });
    }
    function vipHuanxian(jiekou) {
        api.hideProgress();
        var dlna = $api.byId('dlna');
        dlna.style = "display:none";
        var param = {
            rect: {
                x: 0,
                y: $api.dom('header').offsetHeight + 50,
                w: 'auto',
                h: 'auto',
            }, //w,h等于0代表横向满屏和纵向满屏
            fixedOn: "", //浏览器依附在哪个window,不传或传空 为 当前Window，默认当前当前Window
            fixed: true, //浏览器是否随frame或Window滑动，默认当前当前true
            url: jiekou + api.pageParam.srcUrl, //要加载的url,可选项
            browserBg: "#f0f0f0", //可选
            timeout: 7, //超时时间,指加载页面完毕后等待多少秒，超过这个时间还没收到视频地址，则认为解析视频地址失败。默认7秒

        };
        uzmoduledemo.openView(param, function(ret, err) {
            //alert(JSON.stringify(ret));
            if (ret.result == '1') {
                TVURL = ret.VideoUrl;
                var dlna = $api.byId('dlna');
                dlna.style = "display:block";
                uzmoduledemo.closeView( function(ret, err){
                    //alert(JSON.stringify(ret));
                });
            }
        });
        /*var browser1 = api.require('androidBrowser');
        browser1.loadUrl({
            url: jiekou + api.pageParam.srcUrl
        });*/
        var browser = api.require('webBrowser');
        browser.loadUrl({
            url: jiekou + api.pageParam.srcUrl
        });
    }
    function record_play_history() {
        //alert(api.pageParam.title);
        api.ajax({
            url: localStorage.getItem('api_url')+'/login/login/jilu?uid=3',
            data: {
                values: {
                    uid: localStorage.getItem('user_id'),
                    title: api.pageParam.title,
                    url: api.pageParam.srcUrl,
                    time: new Date().format('yyyy-MM-dd hh:mm:ss'),
                    ping: getPing(api.pageParam.srcUrl)
                }
            }
        }, function(ret, err) {});
    }
    function getPing(url) {
        if (url.indexOf("youku.com") > 0) {
            return '优酷';
        } else if (url.indexOf("iqiyi.com") > 0) {
            return '爱奇艺';
        } else if (url.indexOf("v.qq.com") > 0) {
            return "腾讯"
        } else if (url.indexOf("tv.sohu.com") > 0) {
            return "搜狐"
        } else if (url.indexOf("v.qq.com") > 0) {
            return "pptv"
        } else if (url.indexOf("pptv.com") > 0) {
            return "芒果"
        } else if (url.indexOf("mgtv.com") > 0) {
            return "M1905"
        } else if (url.indexOf("1905.com") > 0) {
            return "暴风"
        } else if (url.indexOf("baofeng.com") > 0) {
            return "乐视"
        } else if (url.indexOf("le.com") > 0) {
            return "风行"
        } else if (url.indexOf("xxooweizhiurl.com") > 0) {
            return "未知"
        }
    }
    //搜索设备
    function browse() {
        api.showProgress({
            title: '搜索设备中……',
            text: '请稍等...',
            modal: true
        });
        //var param = {browseType:1};
        var param = {
            browseType: 0
        };
        leCastDemo.browse(param, function(ret, err) {
            if (ret.result == '1') {
                //alert(JSON.stringify(ret));
                console.log(JSON.stringify(ret));
                var arr = [];
                var deviceList = ret.deviceList;
                for (var i = 0; i < ret.deviceList.length; i++) {
                    arr.push(ret.deviceList[i]["name"]);
                }
                console.log('Arr='+arr);
                if(arr.length > 0){
                    api.actionSheet({
                        title: '请选择接收设备',
                        cancelTitle: '取消',
                        buttons: arr
                    }, function(ret, err) {
                        var index = ret.buttonIndex;
                        uid1 = deviceList[index - 1]['ip'];
                        stopBrowse();//停止搜索方法
                        connect();//链接设备
                    });
                }else {

                    api.hideProgress();
                    api.toast({
                        msg: '未搜索到其他设备',
                        duration: 1000
                    });
                }
                api.hideProgress();
            }


        });
    }
    //停止搜索附近的接收端设备
    /*
      因为browse方法会不停的搜索，所以要在适当的时候调用stopBrowse方法
      返回字段说明：
          {result:1
          }
      */
    function stopBrowse() {
        leCastDemo.stopBrowse(function(ret, err) {
            //alert(JSON.stringify(ret));
        });
    }
    //通过uid连接,在browse回调中获取搜索到的uid
    function connect() {
        //var param = {serviceInfoUid: uid1}; //传入uid  1474652758755455217
        var param = {serviceInfoIp:uid1};//传入Ip
        leCastDemo.connect(param, function(ret, err) {
            //alert(JSON.stringify(ret));
            if (ret) {
                stopBrowse();
                //startMirror();
                play();
            }
        });
    }
    //播放媒体
    function play() {
        //var wd = $api.val($api.byId('url1'));
        //console.log(wd);
        var param = {
            directPlay: 0, //是否直接播放，1无需连接设备直接播放(但需要先搜索)，0需连接设备(默认)
            connectedUid: uid1, //直接播放的设备的uid(需要先搜索获取)，directPlay为1时该字段必须要传
            screenCode: "", //有些屏幕需要投屏码的就要传，可选项
            mediaType: 2, //媒体类型：数字型，1音乐,2视频，3图片
            localUrl: "", //本地媒体路径，支持widget://,fs://,安卓系统标准文件路径
            netUrl: TVURL, //网络文件路径,http://...
            //这两个url必须要传一个，两个都传时只认localUrl
        };
        leCastDemo.play(param, function(ret, err) {
            if (ret.status != 25) { //播放进度已更新不弹窗，避免弹窗过多
                //alert(JSON.stringify(ret));
                //var msg = JSON.stringify(ret.msg);
                api.toast({
                    msg: '开始播放',
                    duration: 1000
                });
                var dlna = $api.byId('dlna');
                dlna.style = "display:none";
            }
        });
    }
</script>
</html>
